<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="900px" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="auto">
        <!-- 使用row和col实现一行两列布局 -->
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="工序编号" prop="procedureCode">
              <el-input v-model="formData.procedureCode" placeholder="请输入工序编号" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工序名称" prop="procedureName">
              <el-input v-model="formData.procedureName" placeholder="请输入工序名称" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-row :gutter="20">-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="是否追溯" prop="isTraceabilityCode">-->
<!--              <el-switch v-model="formData.isTraceabilityCode" active-text="是" inactive-text="否" style="width: 100px;" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="追溯规则" prop="traceCodeRules">-->
<!--              <el-input v-model="formData.traceCodeRules" placeholder="请输入追溯规则" style="width: 100%" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
<!--        <el-row :gutter="20">-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="是否委外" prop="isOutsourcing">-->
<!--              <el-switch v-model="formData.isOutsourcing" active-text="是" inactive-text="否" style="width: 100px;" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="formData.status">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)" :key="parseInt(dict.value)"
                  :label="parseInt(dict.value)">
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
<!--        </el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="排序字段" prop="sort">-->
<!--              <el-input v-model="formData.sort" placeholder="请输入排序字段" style="width: 100%" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="绑定工装" prop="isTooling">-->
<!--              <el-switch v-model="formData.isTooling" active-text="是" inactive-text="否" style="width: 100px;" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="描述">
              <Editor v-model="formData.description" :min-height="192" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as ProcedureApi from '@/api/basic/procedure';
import Editor from '@/components/Editor';
import { getDictDatas,getDictDataLabel, DICT_TYPE } from '@/utils/dict'

export default {
  name: "ProcedureForm",
  components: {
    Editor,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        procedureCode: undefined,
        procedureName: undefined,
        isTraceabilityCode: false,
        traceCodeRules: undefined,
        isOutsourcing: false,
        status: undefined,
        sort: undefined,
        isTooling: false,
        description: undefined,
      },
      // 表单校验
      formRules: {
        status: [{ required: true, message: '状态', trigger: 'blur' }],
        sort: [{ required: true, message: '排序字段不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await ProcedureApi.getProcedure(id);
          const data = res.data;
          // 转换数值类型的布尔字段为实际布尔值
          this.formData = {
            ...data,
            isTraceabilityCode: Boolean(data.isTraceabilityCode),
            isOutsourcing: Boolean(data.isOutsourcing),
            isTooling: Boolean(data.isTooling)
          };
          this.dialogTitle = "编辑工序";
        } finally {
          this.formLoading = false;
        }
      } else {
        this.dialogTitle = "新增工序";
      }
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await ProcedureApi.updateProcedure(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await ProcedureApi.createProcedure(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        procedureCode: undefined,
        procedureName: undefined,
        isTraceabilityCode: false,
        traceCodeRules: undefined,
        isOutsourcing: false,
        status: 1,
        sort: undefined,
        isTooling: false,
        description: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
